package com.fortune.service.impl;

import com.fortune.common.paging.PagingData;
import com.fortune.constants.enums.MStore;
import com.fortune.dao.MDataAnalysisDao;
import com.fortune.dao.MStoreDao;
import com.fortune.dto.in.mDataAnalysis.MProductDetailListAllDto;
import com.fortune.dto.in.mDataAnalysis.MProductStoreDetailListDto;
import com.fortune.dto.in.mDataAnalysis.OrderCollectionDto;
import com.fortune.entity.MProductStockDetailAll;
import com.fortune.entity.MProductStockDetailEvery;
import com.fortune.entity.MProductStockDetailInfo;
import com.fortune.entity.MUserInfo;
import com.fortune.service.MDataAnalysisService;
import com.fortune.vo.mDataAnalysis.Dearler;
import com.fortune.vo.mDataAnalysis.MProduct;
import com.fortune.vo.mStoreInit.MInitStoreName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@Service
public class MDataAnalysisServiceImpl implements MDataAnalysisService {
	@Autowired
	private MDataAnalysisDao mDataAnalysisDao;
	@Autowired
	private MStoreDao mStoreDao;
	@Override
	public PagingData<MProductStockDetailInfo> mProductStoreDetailList(MUserInfo mUserInfo, MProductStoreDetailListDto listDto) {
		List<MProductStockDetailInfo> list = mDataAnalysisDao.list(mUserInfo, listDto);
		String mid = mUserInfo.getmId();
		for (MProductStockDetailInfo mProductStockDetailInfo : list) {
			String number = mDataAnalysisDao.getNumber(mProductStockDetailInfo.getType(), mid, mProductStockDetailInfo.getTargetId());
			mProductStockDetailInfo.setNumber(number);
		}
		int count = mDataAnalysisDao.count(mUserInfo, listDto);
		PagingData<MProductStockDetailInfo> pagingData = new PagingData<>(list, count);
		return pagingData;
	}
	@Override
	public List<Map<String, Object>> orderCollection(MUserInfo mUserInfo,OrderCollectionDto orderCollectionDto) {
		List<MProduct> pName = mDataAnalysisDao.pName(mUserInfo);
		Map<String,Object> map = new LinkedHashMap<>();
		map.put("num", "编号");
		map.put("dealer", "经销商");
		for (MProduct mProduct : pName) {
			map.put(mProduct.getId().toString(), mProduct.getName());
		}
		map.put("sum", "合计");
		List<Map<String,Object>> list = new ArrayList<>();
		list.add(map);
		List<Dearler> dids = mDataAnalysisDao.dealers(mUserInfo, orderCollectionDto);
		for(Dearler dearler:dids){
			Map<String, Object> countMap = new LinkedHashMap<>();
			countMap.put("num", dearler.getNumber());
			countMap.put("dealer", dearler.getName());
			int sum = 0;
			for (MProduct mProduct: pName) {
				int count = mDataAnalysisDao.orderCount(mUserInfo, orderCollectionDto, dearler.getDid(),mProduct.getId());
				sum+=count;
				countMap.put(mProduct.getId().toString(), count);
				//System.err.println(count+mProduct.getName());
			}
			countMap.put("sum", sum);
			list.add(countMap);
		}
		//System.out.println(list);
		return list;
	}

	@Override
	public List<Map<String, Object>> mProductStoreDetailAllList(String companyId) {
		List<MInitStoreName> stores = mStoreDao.queryStoreNameList(companyId);
		Map<String,Object> map = new LinkedHashMap<>();
		map.put("num", "编号");
		map.put("name", "名称");
		for (MInitStoreName mStore : stores) {
			map.put(mStore.getId().toString(), mStore.getName());
		}
		map.put("sum", "合计");
		List<Map<String,Object>> list = new ArrayList<>();
		list.add(map);
		List<MProductStockDetailAll> productStockDetailAlls = mDataAnalysisDao.detailAlllist(companyId);

		for(int i = 0; i < productStockDetailAlls.size(); i++){
			Map<String, Object> countMap = new LinkedHashMap<>();
			countMap.put("num", productStockDetailAlls.get(i).getNumber());
			countMap.put("name", productStockDetailAlls.get(i).getName());
			List<MProductStockDetailEvery> storeList = productStockDetailAlls.get(i).getCounts();
			int sum = 0;
			for (MInitStoreName store: stores) {
				int count = mDataAnalysisDao.orderMCount(productStockDetailAlls.get(i).getProductId(), companyId,store.getId());
				sum+=count;
				countMap.put(store.getId().toString(), count);
			}
//			for (MProductStockDetailEvery productCount: storeList) {
//				Float count = productCount.getCount();
//				sum+=count;
//				countMap.put(productCount.getStoreId().toString(), count);
//			}
			countMap.put("sum", sum);
			list.add(countMap);
		}
		return list;



//		List<MProductStockDetailAll> list = mDataAnalysisDao.detailAlllist(companyId, listDto);
//		for (int i = 0; i < list.size(); i++){
//			List<MProductStockDetailEvery> produceList = list.get(i).getCounts();
//			float num = 0;
//			for (int j = 0; j < produceList.size(); j++){
//				num += produceList.get(j).getCount();
//			}
//			list.get(i).setAllCount(num);
//		}
//		int count = mDataAnalysisDao.detailAlllistCount(companyId, listDto);
//		PagingData<MProductStockDetailAll> pagingData = new PagingData<>(list, count);
//		return pagingData;
	}

}
